/*
 * TextImp.cpp
 *
 *  Created on: Apr 27, 2009
 *      Author: wrice
 */
//#include ".h"
#include "DOMCoreImp.h"
namespace DOMCoreImp {

TextImp::TextImp(DOMString data, DocumentImp * ownerDocument)
: CharacterDataImp( "#text", data, ownerDocument )
{
}

unsigned short TextImp::getNodeType() {
	return Node::TEXT_NODE;
}

Text * TextImp::splitText(unsigned long offset) throw(DOMExceptionPtr) {
	if( offset > getLength() )
		throw DOMExceptionPtr(new DOMExceptionImp(DOMException::INDEX_SIZE_ERR));
	assert( getOwnerDocument() );

	const unsigned long lengthForLatterPart = getLength() - offset;
	DOMString latterPart = substringData( offset, lengthForLatterPart );
	Text * newText = getOwnerDocumentImp()->createTextNodeImp( latterPart );

	deleteData( offset, lengthForLatterPart );

	Node * parent = getParentNode();
	if( NULL != parent ) {
		Node * nextSibling = getNextSibling();
		if( NULL != nextSibling )
			parent->insertBefore( newText, nextSibling );
		else
			parent->appendChild( newText );
	}

	return newText;
}

}

